package cn.com.jtang.wedding.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.jtang.wedding.model.AdminUser;
import cn.com.jtang.wedding.model.Charge;
import cn.com.jtang.wedding.model.History;
import cn.com.jtang.wedding.model.User;
import cn.com.jtang.wedding.service.AdminUserManager;
import cn.com.jtang.wedding.service.HistoryManager;
import cn.com.jtang.wedding.service.UserManager;

import com.opensymphony.xwork2.Preparable;

public class HistoryAction extends BaseAction implements Preparable {

	private HistoryManager historyManager;
	private History history;
	private AdminUser user;
	private UserManager userManager;
	private AdminUserManager adminUserManager;
	private AdminUser adminUser;

	private Long hid;
	private String id;
	private String count; // 必填 购买数量
	private String price; // 必填 购买价格
	private String time; // 必填 购买时间
	private Long parentId; // 必填 上级管理员的ID
	private Long userId; // 必填 购买者ID
	private String uid;

	private String userName;
	private String adminUserName;
	private String sellerName;

	private String[] chb;

	private List<History> historys;
	Map<String, Object> map = new HashMap();

	public HistoryManager getHistoryManager() {
		return historyManager;
	}

	public void setHistoryManager(HistoryManager historyManager) {
		this.historyManager = historyManager;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}

	public String insert() throws ParseException {
		if (count != null && price != null && userName != null) {
			history = new History();
			parentId = getCurrentAdminUser().getId();
			user = adminUserManager.queryByAdminAccount(userName);
			if(user==null){
				return "notExist";
			}
			userId = user.getId();
			history.setCount(Double.parseDouble(count));
			history.setPrice(Double.parseDouble(price));
			history.setTime(new Date());
			history.setParentId(parentId);
			if (userId != null) {
				history.setUserId(userId);
			}
			adminUser = getCurrentAdminUser();
			if (adminUser.getAdminType().equals(Long.valueOf(1))
					|| adminUser.getTimes() >= (Double.valueOf(count)
							.longValue())) {
				historyManager.add(history);
				user.setTimes(user.getTimes()
						+ Double.valueOf(count).longValue());
				adminUserManager.update(user);
				adminUser.setTimes(adminUser.getTimes()-Double.valueOf(count).longValue());
				adminUserManager.update(adminUser);
				return SUCCESS;
			}
		}
			return "purchaseError";
	}

	public String delete() {
		history = new History();
		if (chb != null) {
			for (int i = 0; i < chb.length; i++) {
				hid = new Long(chb[i]).longValue();
				history = historyManager.queryById(hid);
				historyManager.delete(history);
			}
		}
		return SUCCESS;
	}

	public String deleteforSingle() {
		history = new History();
		if (id != null) {
			hid = new Long(id).longValue();
			history = historyManager.queryById(hid);
			historyManager.delete(history);
		}
		return SUCCESS;
	}

	public String detailHistory() {
		history = new History();
		adminUser = new AdminUser();
		hid = new Long(id).longValue();
		history = historyManager.queryById(hid);
		userName = adminUserManager.queryById(history.getUserId())
				.getAdminUserName();
		adminUserName = adminUserManager.queryById(history.getParentId())
				.getAdminUserName();
		sellerName = adminUserManager.queryById(history.getParentId())
				.getAdminUserName();
		return SUCCESS;
	}

	public String getAll() {
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		map.put("parentId", getCurrentAdminUser().getId());
		int count = historyManager.getCount(map);
		historys = new ArrayList<History>();
		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount = pageCount == 0 ? 1 : pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		historys = historyManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum);
		return SUCCESS;
	}

	public String buyHistoryList() {
		pageNum = (pageNum) <= 0 ? 1 : pageNum;
		map.put("userId", getCurrentAdminUser().getId());
		int count = historyManager.getCount(map);

		pageCount = (int) Math.ceil((double) count / (double) pageSize);
		pageCount = pageCount == 0 ? 1 : pageCount;
		pageNum = pageNum > pageCount ? pageCount : pageNum;
		historys = historyManager.queryAll(map, pageSize * (pageNum - 1),
				pageSize * pageNum);
		return SUCCESS;
	}

	public History getHistory() {
		return history;
	}

	public void setHistory(History history) {
		this.history = history;
	}

	public Long getHid() {
		return hid;
	}

	public void setHid(Long hid) {
		this.hid = hid;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String[] getChb() {
		return chb;
	}

	public void setChb(String[] chb) {
		this.chb = chb;
	}

	public List<History> getHistorys() {
		return historys;
	}

	public void setHistorys(List<History> historys) {
		this.historys = historys;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public AdminUserManager getAdminUserManager() {
		return adminUserManager;
	}

	public void setAdminUserManager(AdminUserManager adminUserManager) {
		this.adminUserManager = adminUserManager;
	}

	public AdminUser getAdminUser() {
		return adminUser;
	}

	public void setAdminUser(AdminUser adminUser) {
		this.adminUser = adminUser;
	}

	public String getAdminUserName() {
		return adminUserName;
	}

	public void setAdminUserName(String adminUserName) {
		this.adminUserName = adminUserName;
	}

	public AdminUser getUser() {
		return user;
	}

	public void setUser(AdminUser user) {
		this.user = user;
	}

	public String getSellerName() {
		return sellerName;
	}

	public void setSellerName(String sellerName) {
		this.sellerName = sellerName;
	}

	public Long getLastTime() {//剩余次数
		return getCurrentAdminUser().getTimes();
	}


}
